//
//  RegisterViewController.m
//  Closer
//
//  Created by zhangkai on 16/3/10.
//  Copyright © 2016年 Closer_All. All rights reserved.
//

#import "RegisterViewController.h"
#import "LeanCloudTools.h"
#import "User.h"
@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameText;
@property (weak, nonatomic) IBOutlet UIView *usernameLine;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIView *passwordLine;

- (IBAction)registerButton:(UIButton *)sender;
- (IBAction)backButton:(UIButton *)sender;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.usernameText addTarget:self action:@selector(requestMobilePhoneVerifyNumber:) forControlEvents:UIControlEventEditingDidEnd];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)requestMobilePhoneVerifyNumber:(UITextField*)textFeild{

    
}
#pragma mark 注册
- (IBAction)registerButton:(UIButton *)sender {
  TypeOfLeanCloud  flag=[LeanCloudTools registerUserWith:self.usernameText.text and:self.passwordText.text];
    if (flag==userIsExistedError) {
        [self alertMessage:@"用户存在"];
        return;
       
    }else if (flag==userRegisterSuccess){
        [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.usernameText.text password:self.passwordText.text  withCompletion:^(NSString *username, NSString *password, EMError *error) {
            if (!error) {
                [self alertMessage:@"注册成功"];
                [User shareInstance ].username=username;
            }
        } onQueue:nil];
    }
    else{
        [self alertMessage:@"注册失败"];
    }
}
#pragma mark 返回
- (IBAction)backButton:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}
-(void)alertMessage:(NSString*)message{
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:^{
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }];
}

@end
